<template>
    <div class="box">
        <el-upload
  class="avatar-uploader"
  action="http://182.92.156.134:8080/upload"
  :show-file-list="false"
  :on-success="handleAvatarSuccess"
  ref="upload"
  >
  <img width="100" height="100" v-if="ruleForm.cover_img" :src="ruleForm.cover_img" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm"  class="demo-ruleForm">
  <el-form-item label="课程名称" prop="name">
    <el-input v-model="ruleForm.name"></el-input>
  </el-form-item>
  <el-form-item label="学科分类" prop="type">
    <el-select v-model="ruleForm.type" placeholder="请选择学科">
      <el-option v-for="item in classType.classtype" :key="item.id" :label="item.type" :value="item.type"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item label="课程价格" prop="price">
    <el-input v-model="ruleForm.price"></el-input>
  </el-form-item>
  <el-form-item label="课程老师" prop="teacher">
    <el-input v-model="ruleForm.teacher"></el-input>
  </el-form-item>
  <el-form-item label="请输入课时" prop="time">
    <el-input v-model="ruleForm.time"></el-input>
  </el-form-item>
  <el-form-item label="打折" prop="sale">
    <el-input v-model="ruleForm.sale"></el-input>
  </el-form-item>
  <el-form-item label="视频录制时间" required>
    <el-col :span="11">
      <el-form-item prop="date1">
        <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;"></el-date-picker>
      </el-form-item>
    </el-col>
    <el-col class="line" :span="2">-</el-col>
    <el-col :span="11">
      <el-form-item prop="date2">
        <el-time-picker placeholder="选择时间" v-model="ruleForm.date2" style="width: 100%;"></el-time-picker>
      </el-form-item>
    </el-col>
  </el-form-item>
  <el-form-item label="是否热推" prop="is_hot">
    <el-switch v-model="ruleForm.is_hot"></el-switch>
  </el-form-item>
  <el-form-item label="课程描述" prop="desc">
    <el-input type="textarea" v-model="ruleForm.desc"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
    </div>
</template>
<script>
import {mapState} from "vuex"
  export default {
    data() {
      return {
        ruleForm: {
            cover_img:"",
          name: '',
          date1: '',
          date2: '',
          is_hot: false,
          type: "",
          desc: '',
          price:"",
          teacher:"",
          time:"",
          sale:"1000"
        },
        rules: {
            sale:[
                {required:true,message:'折扣',trigger:'blur'}
            ],
            time:[
                {required:true,message:'请输入课时',trigger:'blur'}
            ],
            price:[
                {required:true,message:'请输入价格',trigger:'blur'},
                {min:1,max:5,message:"不能低于1，不能高于10000",trigger:'blur'}
            ],
          name: [
            { required: true, message: '请输入课程', trigger: 'blur' },
          ],
          teacher:[
                {required:true,message:'请输入课程老师',trigger:'blur'}
            ],
          date1: [
            { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
          ],
          date2: [
            { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
          ],
          type: [
            {required: true, message: '选择课程', trigger: 'change' }
          ],
          desc: [
            { required: true, message: '请填写课程描述', trigger: 'blur' }
          ],
          is_hot:[
              {required:true,message:'请填写课程描述',trigger:'blur'}
          ]
        }
      };
    },
    computed:{
        ...mapState(["classType"])
    },
    methods: {
        handleAvatarSuccess(res) {
        this.ruleForm.cover_img = res.url;
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.ruleForm.cover_img===""){
                alert("封面必须上传")
            }
            else{
                this.$store.dispatch("addClass",{...this.ruleForm,author:this.$store.state.user.nickname,
                views:0,
                isZan:false}).then(res=>{
                    alert("创建成功")
                    this.$refs[formName].resetFields();
                    // this.$refs.upload.clearFiles();
                    this.ruleForm.cover_img=""
                })
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        //  this.$refs.upload.clearFiles();
        this.$refs[formName].resetFields();
        this.ruleForm.cover_img=""
      }
    }
  }
</script>
<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
  .box{
      overflow: hidden;
  }
  .el-form-item{
      display: flex;
      margin-bottom: 20px;
      margin-left: 12px;
  }
  .el-row{
    display: flex;
  }
  
</style>